package com.proactive.webapp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.proactive.webapp.dao.CourseCollectDAO;
import com.proactive.webapp.entity.Course;
import com.proactive.webapp.entity.CourseCollect;

public class CourseCollectDAOImpl implements CourseCollectDAO {
	private DataSource dataSource;
	private Connection conn = null ;
	private ResultSet rs = null ;
	private PreparedStatement smt = null ;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public void insert(CourseCollect courseCollect) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO course_collect(courseID, memberAccount, createDate) VALUES(?,?,NOW())";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, courseCollect.getCid());
			smt.setString(2, courseCollect.getAccount());
			smt.executeUpdate();
			
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}

	@Override
	public void delete(CourseCollect courseCollect) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM course_collect WHERE courseID=? AND memberAccount=?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, courseCollect.getCid());
			smt.setString(2, courseCollect.getAccount());
			smt.executeUpdate();
			
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}

	@Override
	public int countTotal(String account) {
		// TODO Auto-generated method stub
		String sql = "SELECT *,(SELECT COUNT(*) FROM course_report d WHERE a.courseID = d.courseID HAVING COUNT(*)<10) "
				+ "AS reportCount FROM course_collect a JOIN course e ON a.courseID = e.courseID "
				+ "WHERE a.memberAccount = ? AND courseStatus = 1 HAVING reportCount IS NOT NULL";
		int count = 0;
		try {			
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setString(1, account);
			rs = smt.executeQuery();
			rs.beforeFirst();  
			rs.last(); 
			count =rs.getRow();
			
			rs.close();
			smt.close();
			
			
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return count;
	}

	@Override
	public List<Course> getList(String loginAccount, String account, int start, int size) {
		// TODO Auto-generated method stub
		List<Course> courseList = new ArrayList<Course>();
		String sql = "SELECT *,(SELECT COUNT(*) FROM course_comment b WHERE topCommentID IS NULL AND a.courseID = b.courseID) "
				+ "AS commentCount,(SELECT COUNT(*) FROM course_favorite c WHERE a.courseID = c.courseID) "
				+ "AS favoriteCount,(SELECT COUNT(*) FROM course_report d WHERE a.courseID = d.courseID HAVING COUNT(*)<10) "
				+ "AS reportCount,(SELECT COUNT(*) FROM course_collect e WHERE a.courseID = e.courseID AND e.memberAccount = ?) "
				+ "AS collect FROM course_collect a JOIN course f ON a.courseID = f.courseID "
				+ "WHERE a.memberAccount = ? AND courseStatus = 1 HAVING reportCount IS NOT NULL "
				+ "ORDER BY a.createDate DESC LIMIT ?,?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setString(1, loginAccount);
			smt.setString(2, account);
			smt.setInt(3, start);
			smt.setInt(4, size);
			rs = smt.executeQuery();
			while(rs.next()){
				Course course = new Course();
				course.setId(rs.getInt("courseID"));
				course.setTitle(rs.getString("courseTitle"));
				course.setImagePath(rs.getString("courseImage"));
				course.setFilePath(rs.getString("courseFile"));
				course.setDownloadUrl(course.getFilePath().equals("")?"":("download?t=1&id="+course.getId()));
				course.setFileUrl(rs.getString("courseFileUrl"));
				course.setIsFile(rs.getInt("isFile"));
				course.setFileUrl(rs.getString("courseFileUrl"));
				course.setStatus(rs.getInt("courseStatus"));
				course.setViewCount(rs.getInt("courseViewCount"));
				course.setFavoriteCount(rs.getInt("favoriteCount"));
				course.setCommentCount(rs.getInt("commentCount"));
				course.setCollectFlag(rs.getInt("collect")==0?false:true);
				course.setCollectString(course.isCollectFlag()?"已收藏":"收藏");
				course.setCollectString1(course.isCollectFlag()?"none":"inline");
				course.setCollectString2(course.isCollectFlag()?"inline":"none");
				course.setCollectClass(course.isCollectFlag()?"topfocusCollect":"");
				courseList.add(course);
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return courseList;
	}

}
